<?php

namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Comm\CommonController as common;
use App\Http\model\Dictionary;

class Cost extends Model
{
	/**
	 * [serverListModel 服务列表]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function serverListModel(){
		$fields = array(
				'id',
				'parent_id',
				'name'
			);
		$itemObj = Db::table('server_item')
					->select($fields)
					->get();
		$item = json_decode(json_encode($itemObj),true);

		if($item) {
			//整理数组
			$comm = new Common;
			$data = $comm->createTree($item,0,'id','parent_id');
			$result['code'] = 1000;
			$result['msg'] = '成功';
			$result['data'] = $data;
		}else{
			$result['code'] = 1004;
			$result['msg'] = '未获取到数据';
		}
		return json_encode($result);
	}

	/**
	 * [dicModel 添加页字典数据]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function dicModel(){
		$dic = new Dictionary;
		$serv = $dic->contents('id');//服务模式
		$settlement = $dic->contents('id2');//费用结算
		$limitcar = $dic->contents('id3');//下单车辆限制
		if ($serv&&$settlement&&$limitcar) {
			$data['serv'] = $serv;
			$data['settlement'] = $settlement;
			$data['limitcar'] = $limitcar;
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $data;
		}else{
			$result['code'] = 1004;
			$result['msg'] = '查询失败';
		}

		return json_encode($result);
	}
	/**
	 * [addCostModel 添加成本]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 */
	public function addCostModel($data){
  		$data['modpeo'] = session('username');
  		$data['modtime'] = time();
		$res = Db::table('cost')
				->insert($data);
		if ($res) {
			$result['code'] = 1000;
			$result['msg'] = '添加成功';
		}else{
			$result['code'] = 1004;
			$result['msg'] = '添加失败';
		}

		return json_encode($result);
	}	
	/**
	 * [cityListModel 已关联城市列表]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @param    [type]     $costId [description]
	 * @return   [type]             [description]
	 */
	public function cityListModel($costId){
		$city = Db::table('cost')
					->where('id',$costId)
					->select('city')
					->first();
		$cityArr = json_decode(json_encode($city),true);
		$data = explode(',', $cityArr['city']);
		$result['code'] = 1000;
		$result['msg'] = '成功';
		$result['data'] = $data;
		return json_encode($result);
	}
	/**
	 * [relationCityModel 关联城市]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function relationCityModel($costId,$citystr){
		$data['city'] = $citystr;
		$res = Db::table('cost')
				->where('id',$costId)
				->update($data);
		if ($res) {
			$result['code'] = 1000;
			$result['msg'] = '关联成功';
		}else{
			$result['code'] = 1004;
			$result['msg'] = '关联失败';
		}

		return json_encode($result);
	}

	/**
	 * [relationCarModel 关联车系]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function relationCarModel($serid,$costId){
		$data['serid'] = $serid;
		$res = Db::table('cost')
				->where('id',$costId)
				->update($data);
		if ($res) {
			//查询服务模式
			$pattern = Db::table('cost')
					->where('id',$costId)
					->select('pattern')
					->first();
			$patternArr = json_decode(json_encode($pattern),true);
			$dic = new Dictionary;
			$serv = $dic->dictionary($patternArr['pattern']);
			$result['code'] = 1000;
			$result['msg'] = '关联成功';
			$result['data'] = $serv;
		}else{
			$result['code'] = 1004;
			$result['msg'] = '关联失败';
		}

		return json_encode($result);
	}

	/**
	 * [relationAlgorModel 发布算法]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function relationAlgorModel($data){
		
		$res = Db::table('history_algorithm')
				->insert($data);
		if ($res) {
			$result['code'] = 1000;
			$result['msg'] = '添加成功';
		}else{
			$result['code'] = 1004;
			$result['msg'] = '添加失败';
		}

		return json_encode($result);
	}

	/**
	 * [findCostModel 成本列表]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function findCostModel($where){

		$fields = array(
				'cost.*',
				'car_series.name as sername'
			);
		$costObj = Db::table('cost')
						->leftJoin('car_series','car_series.id','=','cost.serid')
						->where($where)
						->select($fields)
						->orderBy('modtime','desc')
						->get();
		$cost = json_decode(json_encode($costObj),true);
		if ($cost) {
			$dic = new Dictionary;
			
			//查询城市
			foreach ($cost as $key => $value) {
				//字典信息
				$pattern = $dic->dictionary($value['pattern']);
				$settlement = $dic->dictionary($value['settlement']);
				$limitcar = $dic->dictionary($value['limitcar']);
				$cost[$key]['pattern'] = $pattern;
				$cost[$key]['settlement'] = $settlement;
				$cost[$key]['limitcar'] = $limitcar;
				$code = explode(',', $value['city']);
				$cityObj = Db::table('region')
							->whereIn('code',$code)
							->select('fullname')
							->get();
				$cityArr = json_decode(json_encode($cityObj),true);
				$cityArr = array_column($cityArr,'fullname');
				$citystr = implode(',',$cityArr);
				$cost[$key]['city'] = $citystr;
			}
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $cost;
		}else{

			$result['code'] = 1004;
			$result['msg'] = '获取数据失败';
		}
		return json_encode($result);
	}
	/**
	 * [deleteCostModel 删除成本]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function deleteCostModel($costId){
		$res = Db::table('cost')
						->whereIn('id',$costId)
						->delete();
		if ($res) {
			$result['code'] = 1000;
			$result['msg'] = '删除成功';
		}else{
			$result['code'] = 1004;
			$result['msg'] = '删除失败';
		}

		return json_encode($result);
	}
	/**
	 * [updateCostModel 修改成本]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @param    [type]     $data [description]
	 */
	public function updateCostModel($data,$costId){
  		$data['modpeo'] = session('username');
  		$data['modtime'] = time();
		$res = Db::table('cost')
				->where('id',$costId)
				->update($data);
		if ($res) {
			$result['code'] = 1000;
			$result['msg'] = '修改成功';
		}else{
			$result['code'] = 1004;
			$result['msg'] = '修改失败';
		}

		return json_encode($result);
	}	

	/**
	 * [setStatusModel 启用、冻结]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 */
	public function setStatusModel($costId ,$status){
		$data['serstatus'] = $status;
		$data['modpeo'] = session('username');
  		$data['modtime'] = time();
		$res = Db::table('cost')
				->where('id',$costId)
				->update($data);
		if ($res) {
			$result['code'] = 1000;
			$result['msg'] = '修改成功';
		}else{
			$result['code'] = 1004;
			$result['msg'] = '修改失败';
		}

		return json_encode($result);
	}

	/**
	 * [findHistory 查看历史算法]
	 * @Author   孙丽男
	 * @DateTime 2018-06-14
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function findHistoryModel($costId){

		$costObj = Db::table('history_algorithm')
						->where('costid',$costId)
						->get();
		$cost = json_decode(json_encode($costObj),true);
		if ($cost) {
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $cost;
		}else{
			$result['code'] = 1004;
			$result['msg'] = '查询失败';
		}

		return json_encode($result);
	}
}